from typing import Optional
from fastapi import HTTPException
from src.rag.simple_rag import SimpleRAG

# 全局RAG系统实例
_rag_system: Optional[SimpleRAG] = None

def get_rag_system() -> SimpleRAG:
    """
    获取RAG系统实例，用于FastAPI依赖注入
    
    Returns:
        SimpleRAG: RAG系统实例
        
    Raises:
        HTTPException: 当RAG系统未初始化时抛出500错误
    """
    global _rag_system
    if _rag_system is None:
        raise HTTPException(
            status_code=500,
            detail="RAG系统未初始化"
        )
    return _rag_system

def init_rag_system(rag: SimpleRAG) -> None:
    """
    初始化RAG系统
    
    Args:
        rag: RAG系统实例
    """
    global _rag_system
    _rag_system = rag
